layui.use(['layer', 'form'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    $('.login-wrapper').removeClass('layui-hide');

    /* 表单提交 */
    form.on('submit(loginSubmit)', function (obj) {
        var reqData={
            'username':obj.field.username,
            'password':obj.field.password,
            'grant_type':'password',
            'client_id':client_id,
            'client_secret':client_secret,
            'scope':'read write'
        };
        $.ajax({
            url: basePath+"/auth/oauth/token",
            type:"post",
            data: reqData,
            async: false,
            contentType: 'application/x-www-form-urlencoded',
            beforeSend:function(xhr){
                xhr.setRequestHeader("Authorization", 'Basic ' + $.base64.encode(client_id+':'+client_secret));
            },
            success: function (sResponse) {
                if(sResponse.code=="00000"){
                    var username=sResponse.data.information.username;
                    var auth={"token":sResponse.data.token,"expiresIn":sResponse.data.expiresIn,"refreshToken":sResponse.data.refreshToken,"username":username};
                    var date = new Date().getTime();
                    layer.msg('登录成功', {icon: 1, time: 500}, function () {
                        //设置localStorage的值
                        mateLocalStorage.set("auth", JSON.stringify(auth), date + 3800000);
                        location.replace('./index.html');
                    });
                }else{
                    layer.msg('登录失败', {icon: 2, anim: 6});
                }
            },
            error:function(a,b,c){
                layer.msg('登录失败【'+c+'】', {icon: 2, anim: 6});
            }
        });
        return false;
    });

    /* 图形验证码 */
    var captchaUrl = 'http://shiro.easyweb.vip/assets/captcha';
    $('img.login-captcha').click(function () {
        this.src = captchaUrl + '?t=' + (new Date).getTime();
    }).trigger('click');

});
